from PyQt5 import QtWidgets, QtGui, QtCore
import sys
from youdaoApi_UI import Ui_Form
import youdaoapi
import imrcc_qrc_rc


class myYoudao(QtWidgets.QWidget,Ui_Form):
    def __init__(self):
        super(myYoudao,self).__init__()
        self.setupUi(self)

    def beginsearch(self):
        self.tableWidget.setColumnWidth(0,30)
        self.tableWidget.setColumnWidth(1,70)
        self.tableWidget.setColumnWidth(2,250)


        print(self.search_text.text())
        try:
            if(self.search_text.text()==''):
                QtWidgets.QMessageBox.information(self,"提示","请输入查询条件")
                return
            resultList=youdaoapi.query(self.search_text.text())
            self.tableWidget.setRowCount(len(resultList))
            for index, result in enumerate(resultList):
                print(index,'-',result)

                self.tableWidget.setItem(index,0,QtWidgets.QTableWidgetItem(QtGui.QIcon(':/img/icon.png'),''))
                print(result['subtitle'])
                print(result['title'])
                self.tableWidget.setItem(index,1,QtWidgets.QTableWidgetItem(result['subtitle']))
                self.tableWidget.setItem(index , 2, QtWidgets.QTableWidgetItem(str(result['title'])))


            self.tableWidget.setAutoScroll(True)
        except Exception as e:
            print(e)

    def keyPressEvent(self, QKeyEvent):
        print('按下：',QKeyEvent.key())
        if QKeyEvent.key()==QtCore.Qt.Key_Return:
            print('开始搜索')
            self.beginsearch()


if __name__ == '__main__':
    app=QtWidgets.QApplication(sys.argv)
    youdao=myYoudao()
    youdao.show()
    sys.exit(app.exec_())